package com.leetcode.test.hash;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode-cn.com/explore/learn/card/hash-table/204/practical-application-hash-set/806/
 */
public class 只出现一次的数字 {

	@Test
	public void test() {
		Assert.assertEquals(1, singleNumber(new int[]{2, 2, 1}));
		Assert.assertEquals(4, singleNumber(new int[]{4, 1, 2, 1, 2}));
	}

	public int singleNumber(int[] nums) {
		Map<Integer, Integer> map = new HashMap<>();

		for (int val : nums) {
			if (map.containsKey(val)) {
				map.put(val, map.get(val) + 1);
			} else {
				map.put(val, 1);
			}
		}

		for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
			if (entry.getValue() == 1) {
				return entry.getKey();
			}
		}

		return -1;
	}
}
